/* hal_clock.h */
#ifndef __HAL_CLOCK_H__
#define __HAL_CLOCK_H__

#include "hal_clock\hal_clock_KE18F.h"
#include "hal_common.h"

typedef enum
{
    CLOCK_RootClockSourceSel_Disabled  = 0x0, /*!< Disabled. */
    CLOCK_RootClockSourceSel_SCGSlow   = 0x0, /*!< SCG Slow Clock, only for CLKOUT output. */
    CLOCK_RootClockSourceSel_SystemOSC = 0x1, /*!< System OSC (SOSC_CLK). */
    CLOCK_RootClockSourceSel_SlowIRC   = 0x2, /*!< Slow IRC (SIRC_CLK). */
    CLOCK_RootClockSourceSel_FastIRC   = 0x3, /*!< Fast IRC (FIRC_CLK). */
    CLOCK_RootClockSourceSel_SystemPLL = 0x6, /*!< System PLL (SPLL_CLK) */
} CLOCK_RootClockSourceSel_T;

typedef struct
{
    CLOCK_RootClockSourceSel_T MainClockSourceSel; /*!< 系统主时钟源，系统四大时钟均始于此 */
    uint32_t CoreSystemClockDiv; /*!< 从主时钟源分频得到Core/System时钟 */
    uint32_t BusClockDiv;  /*!< 从System时钟分频得到Bus时钟 */
    uint32_t SlowClockDiv; /*!< 从System时钟分频得到Flash时钟 */
} CLOCK_PlatformClockConfig_T;

typedef enum
{
    CLOCK_SystemOSCRangeSel_Low  = 0x1, /*!< Low frequency range selected for the crystal oscillator of 32 kHz to 40 kHz. */
    CLOCK_SystemOSCRangeSel_Mid  = 0x2, /*!< Medium frequency range selected for the crytstal oscillator of 1 Mhz to 8 Mhz. */
    CLOCK_SystemOSCRangeSel_High = 0x3, /*!< High frequency range selected for the crystal oscillator of 8 Mhz to 32 Mhz. */
} CLOCK_SystemOSCRangeSel_T;

typedef enum
{
    CLOCK_SystemOSCPowerSel_LowPower = 0x0, /*!< Configure crystal oscillator for low-power operation. */
    CLOCK_SystemOSCPowerSel_HighGain = 0x1, /*!< Configure crystal oscillator for high-gain operation. */
} CLOCK_SystemOSCPowerSel_T;

typedef enum
{
    CLOCK_SystemOSCXtalMode_ExtRefClk  = 0x0, /*!< External reference clock selected. */
    CLOCK_SystemOSCXtalMode_RawCrystal = 0x1, /*!< Internal crystal oscillator of OSC requested. */
} CLOCK_SystemOSCXtalMode_T;

typedef struct
{
    bool EnabledInVLPxModes; /*!< System OSC is enabled in VLP modes. */
    bool EnabledInSTOPModes; /*!< System OSC is enabled in Stop modes */
    CLOCK_SystemOSCRangeSel_T RangeSel; /*!< Selects the frequency range for the system crystal oscillator (OSC). */
    CLOCK_SystemOSCPowerSel_T PowerModeSel; /*!< Controls the crystal oscillator power mode of operations. */
    CLOCK_SystemOSCXtalMode_T XtalMode; /*!< Selects the source for the external reference clock. */
} CLOCK_SystemOSCConfig_T;

typedef enum
{
    eCLOCK_FastIRCRange_48MHz = 0U,
    eCLOCK_FastIRCRange_52MHz = 1U,
    eCLOCK_FastIRCRange_56MHz = 2U,
    eCLOCK_FastIRCRange_60MHz = 3U,
} CLOCK_FastIRCRange_T;

typedef struct
{
    CLOCK_FastIRCRange_T ClockRange;
    uint32_t AsyncClkOutputDiv1;
    uint32_t AsyncClkOutputDiv2;
    bool enClkOnRunMode;
    bool enClkOnStopMode;
    bool enClkOnVLPxMode;
} CLOCK_FastIRCConfig_T;

/* Platform clocks. */
void CLOCK_GetCurrentPlatformClockConfig(CLOCK_PlatformClockConfig_T *config);
void CLOCK_SetPlatformClockConfigForPowerRUN(CLOCK_PlatformClockConfig_T *config);
void CLOCK_SetPlatformClockConfigForPowerVLPR(CLOCK_PlatformClockConfig_T *config);
void CLOCK_SetPlatformClockConfigForPowerHSRUN(CLOCK_PlatformClockConfig_T *config);

/* CLKOUT output. */
void CLOCK_SetCLKOUTOutputSel(CLOCK_RootClockSourceSel_T selection);

/* Root clock source - System OSC (SOSC). */
void CLOCK_ConfigSystemOSC(CLOCK_SystemOSCConfig_T *config);
void CLOCK_SetSystemOSCAsyncClockDiv(uint32_t div1, uint32_t div2);

/* Root clock source - Slow IRC (SIRC). */

/* Root clock source - Fast IRC (FIRC). */
void CLOCK_ConfigFastIRC(CLOCK_FastIRCConfig_T *config);

/* Root clock source - System PLL (SPLL). */

#endif /* __HAL_CLOCK_H__ */

